pub trait Messenger{
    fn send(&self, msg: &str);
}

pub struct LimitTracker<'a, T: 'a+Messenger>{
    messenger:&'a T,
    value: usize,
    max: usize
}

impl <'a, T:Messenger> LimitTracker<'a, T> {
    pub fn new(messenger: &T, max: usize)->LimitTracker<T>{
        LimitTracker{
            messenger,
            value:0,
            max
        }
    }

    pub fn set_val(&mut self, value:usize){
        self.value = value;
        let pom = self.value as f64 / self.max as f64;
        if pom>=1.0{
            self.messenger.send("Error!");
        }else if pom>=0.9 {
            self.messenger.send("Urgent!");
        }else if pom>=0.75 {
            self.messenger.send("Warning");
        }
    }
}

#[cfg(test)]
mod tests{
    use super::*;
    use std::cell::RefCell;
    struct MockMessenger{
        sent_messages: RefCell<Vec<String>>,
    }

    impl MockMessenger {
        fn new()->MockMessenger{
            MockMessenger{sent_messages: RefCell::new(vec![])}
        }
    }

    impl Messenger for MockMessenger{
        fn send(&self, message: &str) {
            // self.sent_messages.push(String::from(message));
            let mut one_borrow = self.sent_messages.borrow_mut();
            // let mut two_borrow = self.sent_messages.borrow_mut();

            one_borrow.push(String::from(message));
            // two_borrow.push(String::from(message));
        }
    }

    #[test]
    fn it_sends_an_over_75(){
        let mm = MockMessenger::new();
        let mut lt = LimitTracker::new(&mm, 100);
        lt.set_val(80);
        assert_eq!(mm.sent_messages.borrow().len(), 1);
    }

}